package com.ruoyi.neo.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.neo.domain.PartCategory;
import com.ruoyi.neo.repository.PartRepository;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.neo.mapper.PartMapper;
import com.ruoyi.neo.domain.Part;
import com.ruoyi.neo.service.IPartService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 零件信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-12
 */
@Service
public class PartServiceImpl implements IPartService 
{
    @Resource
    private PartMapper partMapper;
    @Resource
    private PartRepository partRepository;
    /**
     * 查询零件信息
     */
    @Override
    public Part selectPartByPartNumber(String partNumber)
    {

        return (Part) partRepository.findPartByNumberInNeo4j(partNumber);
    }

    /**
     * 查询零件信息列表
     * 
     * @param part 零件信息
     * @return 零件信息
     */
    @Override
    public List<Part> selectPartList(Part part)
    {

        return (List<Part>) partRepository.findAllPartsInNeo4j();
    }

    /**
     * 新增零件信息
     * 
     * @param part 零件信息
     * @return 结果
     */
    @Override
    public int insertPart(Part part)
    {
        String partNumber = partRepository.addPartInNeo4j(part.getPartName(),part.getPartNumber(), part.getCategoryId(), part.getDescription());
        return partNumber==null?0:1;
    }

    //判断
    /**
     * 修改零件信息
     * 
     * @param part 零件信息
     * @return 结果
     */
    @Override
    public int updatePartInMysql(Part part)
    {
        part.setUpdateTime(DateUtils.getNowDate());
        return partMapper.updatePart(part);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePartInNeo4j(Part part) {
        Part existingPart = partRepository.findPartByNumberInNeo4j(part.getPartNumber());
        if (existingPart == null) {
            throw new RuntimeException("Part not found for number: " + part.getPartNumber());
        }

        // 获取旧名称
        String oldName = existingPart.getPartName();
        System.out.println(oldName);

        // 更新分类
        int count = partRepository.updatePartInNeo4j(
                oldName,
                part.getPartName(),
                part.getPartNumber(),
                part.getCategoryId(),
                part.getDescription()
        );
        return count;
    }

    /**
     * 批量删除零件信息
     */
    @Override
    public int deletePartByPartNames(String[] partNames)
    {
        try {
            // 删除 MySQL 中的数据
            int mysqlResult = deletePartByPartNamesInMysql(partNames);
            if (mysqlResult <= 0) {
                throw new RuntimeException("MySQL 删除失败，没有记录被删除");
            }
            // 删除 Neo4j 中的数据
            int neo4jResult = deletPartByNamesInNeo4j(partNames);
            if (neo4jResult <= 0) {
                throw new RuntimeException("Neo4j 删除失败，没有记录被删除");
            }
            return mysqlResult;
        } catch (Exception e) {
            // 抛出异常，触发事务回滚
            throw new RuntimeException("删除操作失败，事务已回滚", e);
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePartByPartNamesInMysql(String[] partNames)
    {
        return partMapper.deletePartByPartNames(partNames);
    }
    //批量删除neo4j的零件节点
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletPartByNamesInNeo4j(String[] partNames) {
        try{
            int count  = partRepository.deletePartsInNeo4j(partNames);
            return count;
        }catch (Exception e){
            StackTraceElement ste =e.getStackTrace()[0];
            System.out.println(e.getMessage());
            System.out.println(ste.getClassName());
            System.out.println(ste.getFileName());
            System.out.println(ste.getLineNumber());
            System.out.println(ste.getMethodName());

            return 0;
        }
    }
    /**
     * 删除零件信息信息
     * 
     * @param partId 零件信息主键
     * @return 结果
     */
    @Override
    public int deletePartByPartId(Long partId)
    {
        return partMapper.deletePartByPartId(partId);
    }

    @Override
    public int linkPartAndCategory(String partName, String categoryName) {
        try{
            partRepository.linkPartToCategory(partName,categoryName);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }
}
